<?php

require_once 'Zend/View/Helper/Abstract.php';

class Core_View_Helper_Internationalization_ListLanguageLinks extends Zend_View_Helper_Abstract
{
    /**
     * Returns a list with available languages
     */
    public function listLanguageLinks()
    {
        if ($page = Core_Builder::getPage()) {
            $currentLanguage = $page->getLanguage();
            $availableLanguages = $page->getAvailableLanguages();
            $xhtml = $this->view->getTranslation('You are reading this page in') . ' ' . $this->view->getTranslation(Core_Language::getFullName($currentLanguage)) . '.';

            if (is_array($availableLanguages)) {
                $languageLinks = array();
                $uri           = new Core_Uri();
                $base          = $uri->toString();
                foreach ($availableLanguages as $locale => $name) {
                    if (!empty($locale) && $locale != $currentLanguage) {
                        $url = $base . '/p/language/' . $locale;
                        $languageLinks[] = '<a href="' . $url . '">' . $this->view->getTranslation($name) . '</a>';
                    }
                }

                if (is_array($languageLinks) && count($languageLinks) > 0) {
                    foreach ($languageLinks as $language) {
                        $languageLinksTranslated[] = $this->view->getTranslation($language);
                    }
                    $xhtml .= ' ' . $this->view->getTranslation('This page is also translated into') . ' ' . implode(', ', $languageLinks);
                }
            }
            return '<p>' . $xhtml . '</p>';
        }
    }
}